package com.njupt.util;

class Node
{
	String xh;
	String sessionID;
	Boolean token=true;
	Long start;
    Node next;
    Node(String xh,String sessionID,Long time)
    {
    	this.xh=xh;
    	this.sessionID=sessionID;
    	this.start=time;
    	this.next=null;
    }
    		
}

public class XHPools 
{
	public static Integer maxID=0;
	
	private static Node head=null;
	public static String getxh(String sessionID)
	{
		//检查是否重复请求
	    Node p=XHPools.head;
	    while(p!=null&&(!sessionID.equals(p.sessionID)))
	    {
	    	p=p.next;
	    }
	    if(p!=null)
	    	return p.xh;
	    
	    // 利用未成功注册的学号
	    p=XHPools.head;
	    long current=System.currentTimeMillis();
	    while(p!=null&&(current-p.start)<10*60*1000)
	    {
	    	p=p.next;
	    }
	    if(p!=null)
	    {
	    	p.sessionID=sessionID;
	    	p.start=current;
	    	return p.xh;
	    }
	    
		
	   	XHPools.maxID++;
	    String xh=maxID(XHPools.maxID);
	    Node node=new Node(xh,sessionID,current);
	    node.next=XHPools.head;
	    XHPools.head=node;
	    return xh;
	}

	private static String  maxID( int id)
	{
	   if(id<10) return "XH000"+id;
	   if(id<100) return "XH00"+id;
	   if(id<1000) return "XH0"+id;
	   return "XH"+id;
	}
	
	public static boolean remove(String xh)
	{
		return true;
	}
}
